﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.Glee.Drawing;
using TfsMerge.Interface;
using TfsMerge.Utils;
using Microsoft.Practices.ServiceLocation;

namespace TfsMerge.Control
{
    public partial class TrackViewControl : UserControl
    {
        public bool IsVisibleToolBar
        {
            set
            {
                var field = gViewer.GetType().GetField("toolBar1", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                if (field != null)
                {
                    var toolbar = field.GetValue(gViewer) as ToolBar;
                    if (toolbar != null)
                        toolbar.Visible = value;
                }
            }
        }

        static ITfs Tfs
        {
            get { return ServiceLocator.Current.GetInstance<ITfs>(); }
        }

        public TrackViewControl()
        {
            InitializeComponent();

            gViewer.ZoomFraction = 0.05;
            gViewer.MouseDoubleClick += GViewerMouseDoubleClick;
            gViewer.MouseWheel += GViewerMouseWheel;
        }

        void GViewerMouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
                gViewer.ZoomInPressed();
            else
                gViewer.ZoomOutPressed();
        }

        public void CreateGraph(int changesetId, IEnumerable<ExtendedMerge> merges)
        {
            var g = new Graph("graph");
            g.GraphAttr.NodeAttr.Shape = Shape.Box;
            g.GraphAttr.LayerDirection = LayerDirection.LR;

            foreach (var itm in merges)
            {
                g.AddEdge(GetItemSource(itm), GetItemTarget(itm));
            }

            var currNode = GetCurrNode(g, changesetId);
            if (currNode != null)
                currNode.Attr.Fillcolor = Color.LightGreen;

            gViewer.Graph = g;
        }

        private static Node GetCurrNode(Graph g, int changesetId)
        {
            var node = g.GleeGraph.CollectAllNodes().FirstOrDefault(x => GetIdFromNodeId(x.Id) == changesetId);
            if (node == null)
                return null;

            return g.FindNode(node.Id);
        }

        void GViewerMouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left &&
                gViewer.SelectedObject is Node)
            {
                var id = GetIdFromNodeId((gViewer.SelectedObject as Node).Id);
                if (id.HasValue)
                    Tfs.ShowChangesetDetailsDialog(FindForm(), id.Value);
            }
        }

        private static int? GetIdFromNodeId(string id)
        {
            var itms = id.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            if (itms.Length > 1 && Converter.Str2Int(itms[1]) != null)
            {
                var str2Int = Converter.Str2Int(itms[1]);
                if (str2Int != null) 
                    return str2Int.Value;
            }

            return null;
        }

        private string GetItemSource(ExtendedMerge itm)
        {
            return itm.SourceItem.Item.ServerItem +
                Environment.NewLine +
                itm.SourceChangeset.ChangesetId +
                Environment.NewLine +
                itm.SourceChangeset.CreationDate +
                Environment.NewLine +
                itm.SourceChangeset.Committer;
        }

        private string GetItemTarget(ExtendedMerge itm)
        {
            return itm.TargetItem.Item + 
                Environment.NewLine + 
                itm.TargetChangeset.ChangesetId + 
                Environment.NewLine + 
                itm.TargetChangeset.CreationDate +
                Environment.NewLine +
                itm.TargetChangeset.Committer;
        }

    }
}
